package logic;
import jess.*;
import models.*;

import java.util.ArrayList;
import java.util.Iterator;

public class Performance {
	ArrayList<Person> postulants;
	ArrayList<Results> results;

    public Performance() throws JessException {	
    }
    
    public ArrayList<Person> applicantsFound(Profile profile){
       	ArrayList<Qualities> qualitiesPerson;
    	ArrayList<Person> lista;
    	getPostulants(profile);
    	sumKnowledgesPerson(profile);
    	qualitiesPerson=getQualitiesPerson();
    	sumQualitiesPerson(qualitiesPerson, profile);
    	getSumTotalResults();
    	lista=getBestsPostulants();
    	   	
    	for(int i=0;i<results.size();i++){
    		System.out.println("Id: "+results.get(i).getIdPerson()+" Sum Con: "+results.get(i).getResultKnowledges()+" Sum cual: "+results.get(i).getResultQuality()+" Total: "+results.get(i).getTotal()+"   "+lista.get(i).getFirstName()+" "+lista.get(i).getLastname());
    		
    	}
    	  	
    	return lista;
    }
    
     
    public void getPostulants(Profile profile){
    	postulants=new ArrayList<Person>();
    	ArrayList<Person> lista=Person.obtenerPersonas();
    	ArrayList<String> knowledgesPerson=new ArrayList<String>();
    	ArrayList<String> knowledges=new ArrayList<String>();
    	boolean salir=false;
    	
    	knowledges=profile.getRequiredKnowledges();
    	for(int i=0;i<lista.size();i++){
    		salir=false;
    		knowledgesPerson=lista.get(i).getKnowledges();
    		for(int j=0;j<knowledges.size()&&salir==false;j++){
    			for(int k=0;k<knowledgesPerson.size()&&salir==false;k++){
        			if(knowledges.get(j).equalsIgnoreCase(knowledgesPerson.get(k))){
        			
        				if(!profile.getCity().equalsIgnoreCase("")&&!(profile.getGender().equalsIgnoreCase("None"))){
        					if(lista.get(i).getCity().equalsIgnoreCase(profile.getCity())&&lista.get(i).getGender().equalsIgnoreCase(profile.getGender())){     						
        						postulants.add(lista.get(i));
            					salir=true;
        					}
        				}
        								
        				if(!profile.getCity().equalsIgnoreCase("")&&profile.getGender().equalsIgnoreCase("None")){
    						if(lista.get(i).getCity().equalsIgnoreCase(profile.getCity())){
    							
    							postulants.add(lista.get(i));
            					salir=true;
    						}
    					}
        				if(!profile.getGender().equalsIgnoreCase("None")&&profile.getCity().equalsIgnoreCase("")){
    						if(lista.get(i).getGender().equalsIgnoreCase(profile.getGender())){
    							
    							postulants.add(lista.get(i));
            					salir=true;
    						}
    					}
    					if(profile.getCity().equalsIgnoreCase("")&&(profile.getGender().equalsIgnoreCase("None"))){
    						
    						postulants.add(lista.get(i));
        					salir=true;
    						
    					}
        			     								
        			}     			
        		}   			
    		}    		
    	}   		
    }
    
    
   /*public void sumKnowledgesPerson(Profile profile){
	    results=new ArrayList<Results>();
	   
		ArrayList<String> knowledgesPerson=new ArrayList<String>();
	   	ArrayList<String> knowledges=new ArrayList<String>();
	   	int sum=0;
	   	
	   	knowledges=profile.getRequiredKnowledges();
	   	for(int i=0;i<postulants.size();i++){
	   		knowledgesPerson=postulants.get(i).getKnowledges();
	   		for(int j=0;j<knowledges.size();j++){
	   			for(int k=0;k<knowledgesPerson.size();k++){
	       			if(knowledges.get(j).equals(knowledgesPerson.get(k))){
	       					sum=sum+postulants.get(i).getKnowledgesWeights().get(k);			
	       			}     			
	       		}   			
	   		}
	   		results.add(new Results(postulants.get(i).getId(),sum));
	   	} 
	   	
	   	
   } */
    
    public void sumKnowledgesPerson(Profile profile){
        results=new ArrayList<Results>();
        int suma=0; 	
        Rete engine;
        int sumRequired=0;
        int totalCon=0;
        
 		ArrayList<String> knowledgesPerson=new ArrayList<String>();
 	   	ArrayList<String> knowledgesRequired=new ArrayList<String>();
 	  	   	
	 	   try{
	 		  knowledgesRequired=profile.getRequiredKnowledges();
	 	 	   	for(int i=0;i<postulants.size();i++){
	 	 	   		knowledgesPerson=postulants.get(i).getKnowledges();
	 	 	   		
	 	 	   		for(int j=0;j<knowledgesRequired.size();j++){
	 	 	   			for(int k=0;k<knowledgesPerson.size();k++){
	 	 	   				
	 		 	   			engine = new Rete();          
	 		                engine.batch("logic/Facts2.clp");
	 		                      	
	 		            	engine.add(new KnowledgeRequired(knowledgesRequired.get(j), profile.getRequiredKnowledgesWeights().get(j)));
	 		            	engine.add(new KnowledgePerson(knowledgesPerson.get(k), postulants.get(i).getKnowledgesWeights().get(k)));
	 		            	engine.run();
	 		            	
	 		                Iterator iter1=engine.getObjects(new Filter.ByClass(Integer.class));
	 		                while (iter1.hasNext()) {
	 		                	int num=(int)iter1.next();
	 		                	suma=suma+num;		 		                	
	 		                }
	 		                
	 		                engine.clear();
	 		                 		               	 	   				   			
	 	 	   			}
	 	 	   			sumRequired=sumRequired+ profile.getRequiredKnowledgesWeights().get(j);
	 	 	   		}
	 	 	   		
	 	 	   	    totalCon=(suma*600)/sumRequired;
	 	 	   	    
	 	 	   	    System.out.println("EL VALOR sumaRequired es"+sumRequired);
	 	 	   	    System.out.println("EL VALOR de suma es"+suma);
	 	 	   	    System.out.println("EL VALOR que obtuvo es"+totalCon);
	 	 	   	    
	 	 	   		
	 	 	   		if(postulants.get(i).getYears()>0){
	 	 	   			int num=postulants.get(i).getYears()*10;
	 	 	   			totalCon=totalCon+num;
	 	 	   			System.out.println("EL VALOR por experiencia es "+totalCon);
	 	 	   		}
	 	 	   		
	 	 	   		  	 	   		
	 	 	   		int tmp=pointsCourses(profile.getStudies(), postulants.get(i).getStudies());
	 	 	   	    totalCon=totalCon+tmp;
	 	 	   	    System.out.println("EL VALOR por cursos es "+tmp);
	 	 	   		
	 	 	   		int tmp2 = pointsAge(profile.getAgeMin(), profile.getAgeMax(),postulants.get(i).getAge());
	 	 	   	    totalCon = totalCon +tmp2;
	 	 	   	    System.out.println("EL VALOR por edad es "+tmp2); 	 	   		
	 	 	   	    
	 	 	   	    System.out.println("EL VALOR total por con es "+totalCon+"\n");
	 	 	   		results.add(new Results(postulants.get(i).getId(),totalCon));
	 	 	   		suma=0;
	 	 	   	    sumRequired=0;
	 	 	   	    totalCon=0;
	 	 	   		
	 	 	   	}
	 		   
	 	   }catch(Exception e){
	 		  System.out.println("Error en las reglas2");	
	 		   
	 	   }
	    	
    }
   
   
   public  ArrayList<Qualities> getQualitiesPerson(){
	   ArrayList<String> qualitiesPerson=new ArrayList<String>();
	   ArrayList<Qualities> qualitiesPersonWeights=new ArrayList<Qualities>();
	   Qualities qualityWeigth;
	   
	   for(int i=0;i<postulants.size();i++){	   
		   qualitiesPerson=postulants.get(i).getQualities();
		   qualityWeigth=new Qualities();
		   for(int j=0;j<qualitiesPerson.size();j++){
	
			   qualityWeigth.setIdPerson(postulants.get(i).getId());
			   
			   if(qualitiesPerson.get(j).equals("Comunication")){
				   qualityWeigth.setComunication(postulants.get(i).getQualitiesWeights().get(j));
			   }
			   if(qualitiesPerson.get(j).equals("Computation knowledge")){
				   qualityWeigth.setComputation_knowledge(postulants.get(i).getQualitiesWeights().get(j));
			   }
			   if(qualitiesPerson.get(j).equals("Solving problems")){
				   qualityWeigth.setSolving_problems(postulants.get(i).getQualitiesWeights().get(j));
			   }
			   if(qualitiesPerson.get(j).equals("Interpersonal habilities")){
				   qualityWeigth.setInterpersonal_habilities(postulants.get(i).getQualitiesWeights().get(j));
			   }
			   if(qualitiesPerson.get(j).equals("Group habilities")){
				   qualityWeigth.setGroup_habilities(postulants.get(i).getQualitiesWeights().get(j));
			   }
			   if(qualitiesPerson.get(j).equals("Diviersity")){
				   qualityWeigth.setDiversity(postulants.get(i).getQualitiesWeights().get(j));
			   }
			   if(qualitiesPerson.get(j).equals("Planning")){
				   qualityWeigth.setPlanning(postulants.get(i).getQualitiesWeights().get(j));
			   }
			   if(qualitiesPerson.get(j).equals("Leadership")){
				   qualityWeigth.setLeadership(postulants.get(i).getQualitiesWeights().get(j));
			   }
			   if(qualitiesPerson.get(j).equals("Flexibility")){
				   qualityWeigth.setFlexibility(postulants.get(i).getQualitiesWeights().get(j));
			   }
			   if(qualitiesPerson.get(j).equals("Professionalism")){
				   qualityWeigth.setProfessionalism(postulants.get(i).getQualitiesWeights().get(j));
			   }
			   if(qualitiesPerson.get(j).equals("Possitive actitude")){
				   qualityWeigth.setPossitive_actitude(postulants.get(i).getQualitiesWeights().get(j));
			   }		   
		   }
		   qualitiesPersonWeights.add(qualityWeigth);
		   	   
	   }
	   return qualitiesPersonWeights;
	   
   }
   
   public void sumQualitiesPerson(ArrayList<Qualities> cualidades,Profile profile){
	   Rete engine;
	   int suma;
       
	   QualitiesPriorities priorities= new QualitiesPriorities(profile.getRequiredQualities().get(0),profile.getRequiredQualities().get(1),profile.getRequiredQualities().get(2));
   	
	    
	   try{	
		   for(int i=0;i<cualidades.size();i++){ 	
		       suma=0;     	
		       engine = new Rete();          
		       engine.batch("logic/Facts.clp");		          	
		       engine.add(cualidades.get(i));
		       engine.add(priorities);
		       engine.run();
		       	
	           Iterator iter1=engine.getObjects(new Filter.ByClass(Integer.class));
	           while (iter1.hasNext()) {
	           	int num=(int)iter1.next();
	           	suma=suma+num;
	           	
	           }
	           for(int j=0;j<results.size();j++){
	        	   if(results.get(j).getIdPerson()==cualidades.get(i).getIdPerson()){
	        		   	results.get(j).setResultQuality(suma);
	        	   }
	        		   
	           }
	       }
		   
	   }catch(Exception e){
		   System.out.println("Error en las reglas");		   
	   }
	    
   }
   
   public void getSumTotalResults(){
	   int total;
	   for(int i=0;i<results.size();i++){
		   total=results.get(i).getResultKnowledges()+results.get(i).getResultQuality();
		   results.get(i).setTotal(total);
		   
	   }
   }
   
   
   
   public ArrayList<Person> getBestsPostulants(){
	   ArrayList<Person> lista=new ArrayList<Person>();
	   Results tmpA;
	   Results tmpB;
	   	 	   
	   for(int i=0;i<results.size();i++){
		   for(int j=0;j<results.size();j++){
			   if(results.get(i).getTotal()>results.get(j).getTotal()){
				   tmpA=results.get(i);
				   results.set(i, results.get(j));
				   results.set(j, tmpA);
			   	}
		   
		   }	   
	   }
	   
	   for(int i=0;i<results.size();i++){
		   for(int j=0;j<postulants.size();j++){
			   if(postulants.get(j).getId()==results.get(i).getIdPerson()){
				   	lista.add(postulants.get(j));
			   }
		   }
		   
	   }
	   return lista;	   
   }
   
   
   public int pointsCourses(ArrayList<String> lista, ArrayList<String> lista2){
	   	ArrayList<String> wordsFilter= new ArrayList<String>();
	   	wordsFilter.add("the");
	   	wordsFilter.add("an");
	   	wordsFilter.add("a");
	   	wordsFilter.add("in");
	   	wordsFilter.add("to");
	   	wordsFilter.add("at");
	   	int sum=0;
	   	
	   	
	   	
	   	for (int i=0;i<lista2.size();i++){
	   		for (int j=0;j<wordsFilter.size();j++){
	   			if (lista2.get(i).equalsIgnoreCase(wordsFilter.get(j))){
	   				lista2.remove(i);
	   			}
	   		}
	   	}
	   	
	   	
	   	
	   	for(int i=0;i<lista.size();i++){
	   		for(int j=0;j<lista2.size();j++){
		   			if(lista.get(i).equalsIgnoreCase(lista2.get(j))){
		   					sum=sum+50;
		   			}
		   		
		   	}
	   		
	   	}
	   	
	   	return sum;
   }
   
   public int pointsAge(int min, int max, int age){
	   int promedio = max - min / 2;
	   Double valor;
	   valor = (age-promedio)/20.0;
	   if (valor<0)
		   valor=valor*-1;
	   valor = 1 - valor;
	   valor = 50 * valor;	   
	   return valor.intValue();
   }
   
   
   
   

       
}